/**
 * 
 */
package cc.comac.mom.datahandlers.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipPartBorrowDetail;
import cc.comac.mom.bean.EquipPartBorrowDetailBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipPartBorrowDetailDao;
import cc.comac.mom.dao.entity.EquipWarehouseStockDao;
import cc.comac.mom.entity.EquipPartBorrowDetailEntity;
import cc.comac.mom.entity.EquipWarehouseStockEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

/**
* @Description: 
* @author: fyt 
* @date: 2020年7月17日上午10:43:19
*/
@Component
public class EquipPartBorrowDetailDataHandler extends ServiceImpl<EquipPartBorrowDetailDao, EquipPartBorrowDetailEntity>{

	@Autowired
	private EquipPartBorrowDetailDao equipPartBorrowDetailDao;
	@Autowired
	private EquipWarehouseStockDao equipWarehouseStockDao;
	@Autowired
	private BaseEntityDataUtil 	baseEntityDataUtil;
	
	public ResultBean saveBatch(List<EquipPartBorrowDetail> list) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(list == null || list.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		ArrayList<EquipPartBorrowDetailEntity> listEntity = new ArrayList<EquipPartBorrowDetailEntity>();
		for(EquipPartBorrowDetail item:list) {
			EquipPartBorrowDetailEntity innerData = item.getInnerData();
			if(innerData == null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("数据为空");
				return resultBean;
			}
			if(innerData.getBorrowId() == 0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("领用父id为空");
				return resultBean;
			}
			if(innerData.getSparePartId() == 0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("备件信息为空");
				return resultBean;
			}
			EquipWarehouseStockEntity warehouseStockEntity = equipWarehouseStockDao.selectById(innerData.getWarehouseId());
			if(warehouseStockEntity == null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("备件库存信息不存在");
				return resultBean;	
			}
			if(innerData.getBorrowQty().compareTo(new BigDecimal(warehouseStockEntity.getQuantity()))>0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("领用数量"+innerData.getBorrowQty()+"大于备件库存剩余数量"+warehouseStockEntity.getQuantity());
				return resultBean;	
			}
			baseEntityDataUtil.setBaseEntityData(innerData);
			listEntity.add(innerData);
		}
		boolean saveBatch = saveBatch(listEntity);
		if(!saveBatch) {
			resultBean.setSuccess(false);
			resultBean.setMessage("新增失败");
			return resultBean;
		}
		return resultBean;
	}
	
	public ResultBean updateBatch(List<EquipPartBorrowDetail> list) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(list == null || list.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		ArrayList<EquipPartBorrowDetailEntity> listEntity = new ArrayList<EquipPartBorrowDetailEntity>();
		for(EquipPartBorrowDetail item:list) {
			EquipPartBorrowDetailEntity innerData = item.getInnerData();
			if(innerData == null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("数据为空");
				return resultBean;
			}
			if(innerData.getBorrowId() == 0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("领用父id为空");
				return resultBean;
			}
			if(innerData.getSparePartId() == 0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("备件信息为空");
				return resultBean;
			}
			EquipWarehouseStockEntity warehouseStockEntity = equipWarehouseStockDao.selectById(innerData.getWarehouseId());
			if(warehouseStockEntity == null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("备件库存信息不存在");
				return resultBean;	
			}
			if(innerData.getBorrowQty().compareTo(new BigDecimal(warehouseStockEntity.getQuantity()))>0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("领用数量"+innerData.getBorrowQty()+"大于备件库存剩余数量"+warehouseStockEntity.getQuantity());
				return resultBean;	
			}
			baseEntityDataUtil.updateBaseEntityData(innerData);
			listEntity.add(innerData);
		}
		boolean saveBatch = updateBatchById(listEntity);
		if(!saveBatch) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改失败");
			return resultBean;
		}
		return resultBean;
	}
	
	public ResultBean deleteBatch(List<EquipPartBorrowDetail> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		
		for(EquipPartBorrowDetail item:list) {
			EquipPartBorrowDetailEntity innerData = item.getInnerData();
			if(innerData == null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("数据为空");
				return resultBean;	
			}
			listIds.add(innerData.getId());
		}
		List<EquipPartBorrowDetailEntity> selectBatchIdsList = equipPartBorrowDetailDao.selectBatchIds(listIds);
		if(selectBatchIdsList == null || selectBatchIdsList.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;	
		}
		for(EquipPartBorrowDetailEntity item :selectBatchIdsList) {
			baseEntityDataUtil.deleteBaseEntityData(item);
		}
		boolean updateBatchById = updateBatchById(selectBatchIdsList);
		
		if(!updateBatchById) {
			resultBean.setSuccess(false);
			resultBean.setMessage("删除失败");
			return resultBean;
		}

		return resultBean;
	}

	public List<EquipPartBorrowDetail> getBorrowIds(List<Long> listBorrowIds){
		if(listBorrowIds != null && listBorrowIds.size()>0) {
			QueryWrapper<EquipPartBorrowDetailEntity> queryWrapper = new QueryWrapper<EquipPartBorrowDetailEntity>();
			queryWrapper.eq("row_status", 1);
			queryWrapper.in("borrow_id", listBorrowIds);
			List<EquipPartBorrowDetailEntity> selectList = equipPartBorrowDetailDao.selectList(queryWrapper);
			if(selectList != null && selectList.size()>0) {
				ArrayList<EquipPartBorrowDetail> listEquipPartBorrowDetail = new ArrayList<EquipPartBorrowDetail>();
				for(EquipPartBorrowDetailEntity item:selectList) {
					EquipPartBorrowDetail equipCheckDetail = new EquipPartBorrowDetail();
					equipCheckDetail.setInnerData(item);
					listEquipPartBorrowDetail.add(equipCheckDetail);
				}
				return listEquipPartBorrowDetail;
			}
			return null;
		}
		return null;
		
	}
	
	public ResultDev selectByFields(EquipPartBorrowDetailBean obj) {
		ResultDev resultDev = new ResultDev();
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		obj.setPageNum((obj.getPageNum() - 1) * obj.getPageSize());
		List<EquipPartBorrowDetailBean> listEquipPartBorrowBean = equipPartBorrowDetailDao.selectByFields(obj);
		long countByFields = equipPartBorrowDetailDao.selectCountByFields(obj);
		
		// 将查询总数除以每页数量后向上取整获取总页数
		double pageCount = Math.ceil((double)countByFields / obj.getPageSize());
		resultDev.setEntityList(listEquipPartBorrowBean);
		resultDev.setPageCount((int)pageCount);
		resultDev.setTotalQuantity((int)countByFields);
		return resultDev;
	}
	
}
